<?php
class MHDEV_Plugin_UserOnline extends Zend_Controller_Plugin_Abstract 
{
	public function preDispatch(Zend_Controller_Request_Abstract $request) 
	{
		// vars
		$board_uid			= 0;
		$thread_uid			= 0;
		
		// ip
		$ip					= $_SERVER['REMOTE_ADDR'];
		
		// auth instance
		$auth				= Zend_Auth::getInstance();
		
		// zend_date
		$date				= new Zend_Date();
		
		// settings
		$settings			= Zend_Registry::get('settings');
		
		// request
		$module				= $request->getModuleName();
		$controller			= $request->getControllerName();
		
		if(!$module)	{ $module = 'default'; }
		
		// db
		$userOnline			= new UserOnline();
		$userOnlineCount	= new UserOnlineCount();
		$threads			= new Threads();
		$user				= new User();
		
		// check user-stats
		$fetch				= $userOnlineCount->fetchAll('ip_address = "' . $ip . '" AND DATE_SUB(NOW(), INTERVAL 1 DAY) < date');
		
		// delete inactive user
		$userOnline->delete('DATE_SUB(NOW(), INTERVAL ' . $settings['online_count'] . ' MINUTE) > date');
		
		// who is the user ...
		if($controller == 'threads')
		{
			$board_uid = $request->getParam('uid');
		}
		elseif($controller == 'posts')
		{
			$thread_uid = $request->getParam('uid');
			
			$getThread	= $threads->find($thread_uid);
			
			if($getThread->count() > 0)
			{
				$resThread	= $getThread->toArray();
				$board_uid	= $resThread[0]['board_uid'];
			}
		}
		
		// manage online user
		if($auth->hasIdentity())
		{
			$user_id 	= $auth->getIdentity()->uid;
			$result		= $userOnline->fetchRow('user_uid = ' . $user_id);

			if($result)
			{
				$userOnline->update(array('module' => $module, 'controller' => $controller, 'board_uid' => $board_uid, 'thread_uid' => $thread_uid, 'date' => new Zend_Db_Expr('NOW()')), 'user_uid = ' . $user_id);
			}
			else
			{
				$userOnline->insert(array('module' => $module, 'controller' => $controller, 'board_uid' => $board_uid, 'thread_uid' => $thread_uid, 'user_uid' => $user_id, 'date' => new Zend_Db_Expr('NOW()')));
			}
		}
		else
		{
			$sid 		= Zend_Session::getId();
			$result		= $userOnline->fetchRow('ip_address = "' . $ip . '"');
			
			if($result)
			{
				$userOnline->update(array('module' => $module, 'controller' => $controller, 'board_uid' => $board_uid, 'thread_uid' => $thread_uid, 'date' => new Zend_Db_Expr('NOW()')),'ip_address = "' . $ip . '"');
			}
			else
			{
				$userOnline->insert(array('module' => $module, 'controller' => $controller, 'board_uid' => $board_uid, 'thread_uid' => $thread_uid, 'ip_address' => $ip, 'date' => new Zend_Db_Expr('NOW()'), 'user_uid' => 0));
			}
		}
		
		// log user stats
		if($fetch->count() == 0)
		{
			$referer		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
			$language		= isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
			$user_agent		= isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
			$request_uri	= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
			
			$userOnlineCount->insert(array(
				'ip_address'	=> $ip,
				'host_address'	=> gethostbyaddr($ip),
				'referer'		=> $referer,
				'language'		=> $language,
				'resolution'	=> '',
				'browser'		=> $user_agent,
				'page'			=> $request_uri,
				'date'			=> new Zend_Db_Expr('NOW()')
			));
		}
		
		// get allready logged in users with cookies ...
		if(!$auth->hasIdentity() && isset($_COOKIE['mhdev_hash1']) && isset($_COOKIE['mhdev_hash2']))
		{
			$check_cookie	= $user->fetchAll('uid > 0 AND is_active=1 AND hash1 = "' . $_COOKIE['mhdev_hash1'] . '" AND hash2 = "' . $_COOKIE['mhdev_hash2'] . '"');
			
			if($check_cookie->count() == 1)
			{
				
			}
		}
	}
}
?>